<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
	<xsl:template match="p">
		<p>
			<xsl:apply-templates/>
		</p>
	</xsl:template>

	<xsl:template match="head">
		<xsl:choose>
			<xsl:when test="parent::scopecontent or parent::bibliography">
				<h3>
					<xsl:apply-templates/>
				</h3>
			</xsl:when>
			<xsl:when test="parent::descgrp or parent::bioghist">
				<h2>
					<xsl:apply-templates/>
				</h2>
			</xsl:when>
			<xsl:when test="parent::list or parent::chronlist">
				<li style="font-weight:bold;">
					<xsl:apply-templates/>
				</li>
			</xsl:when>
			<xsl:otherwise>
				<h3>
					<xsl:apply-templates/>
				</h3>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="abstract">
		<div class="commentary_piece">
			<hr/>
			<a name="#{generate-id(.)}"/>
			<xsl:choose>
				<xsl:when test="string(normalize-space(@label))">
					<h2>
						<xsl:value-of select="@label"/>
					</h2>
				</xsl:when>
				<xsl:otherwise>
					<h2>
						<xsl:text>Abstract</xsl:text>
					</h2>
				</xsl:otherwise>
			</xsl:choose>
			<div class="essay_section">
				<xsl:apply-templates/>
			</div>
		</div>
	</xsl:template>

	<xsl:template match="descgrp | bioghist">
		<div class="commentary_piece">
			<xsl:if test="not(parent::archdesc)">
				<hr/>
			</xsl:if>
			<xsl:apply-templates
				select="head | scopecontent | bibliography | odd[@type='doc_author']"/>
			<xsl:if test="string(normalize-space(odd[@type='doc_author'])) and ancestor::c">
				<div>
					<xsl:text>By </xsl:text>
					<xsl:value-of select="odd[@type='doc_author']"/>
				</div>
			</xsl:if>
		</div>
	</xsl:template>

	<xsl:template match="scopecontent">
		<div class="essay_section">
			<a name="{generate-id(.)}"/>
			<xsl:apply-templates/>
			<p>
				<xsl:variable name="section_id">
					<xsl:if test="ancestor::descgrp">
						<xsl:value-of select="generate-id(ancestor::descgrp)"/>
					</xsl:if>
					<xsl:if test="ancestor::bioghist">
						<xsl:value-of select="generate-id(ancestor::bioghist)"/>
					</xsl:if>
				</xsl:variable>
				<xsl:if test="not(parent::node()/parent::archdesc)">
					<a href="#{$section_id}">Back to top</a>
				</xsl:if>
			</p>
		</div>
	</xsl:template>

	<xsl:template match="odd[@type='doc_author']">
		<h3>
			<xsl:value-of select="."/>
		</h3>
	</xsl:template>

	<xsl:template match="emph">
		<xsl:choose>
			<xsl:when test="@render='italic'">
				<i>
					<xsl:apply-templates/>
				</i>
			</xsl:when>
			<xsl:when test="@render='bold'">
				<b>
					<xsl:apply-templates/>
				</b>
			</xsl:when>
			<xsl:when test="@render='underline'">
				<u>
					<xsl:apply-templates/>
				</u>
			</xsl:when>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="title">
		<xsl:choose>
			<xsl:when test="@render='italic'">
				<i>
					<xsl:apply-templates/>
				</i>
			</xsl:when>
			<xsl:when test="@render='bold'">
				<b>
					<xsl:apply-templates/>
				</b>
			</xsl:when>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="ptr">
		<xsl:variable name="current_target" select="@target"/>

		<a name="{$current_target}_ref"/>

		<a href="#{$current_target}" class="pointer">
			<!--<xsl:value-of select="@target"/>-->
			<xsl:choose>
				<xsl:when test="ancestor::descgrp">
					<xsl:for-each select="ancestor::descgrp/descendant::ptr">
						<xsl:if test="@target = $current_target">
							<xsl:value-of select="position()"/>
						</xsl:if>
					</xsl:for-each>
				</xsl:when>
				<xsl:when test="ancestor::bioghist">
					<xsl:for-each select="ancestor::bioghist/descendant::ptr">
						<xsl:if test="@target = $current_target">
							<xsl:value-of select="position()"/>
						</xsl:if>
					</xsl:for-each>
				</xsl:when>
			</xsl:choose>
		</a>
	</xsl:template>

	<xsl:template match="ref">
		<a name="{@target}_ref"/>

		<a href="#{@target}" class="pointer">
			<xsl:value-of select="."/>
		</a>
	</xsl:template>

	<xsl:template match="note">
		<xsl:variable name="current_id" select="@id"/>
		<div style="display:table;width:100%;">
			<div class="note-symbol">
				<a href="#{@id}_ref">
					<xsl:choose>
						<xsl:when test="ancestor::descgrp">
							<xsl:for-each select="ancestor::descgrp/descendant::ptr">
								<xsl:if test="@target = $current_id">
									<xsl:value-of select="position()"/>
								</xsl:if>
							</xsl:for-each>
						</xsl:when>
						<xsl:when test="ancestor::bioghist">
							<xsl:for-each select="ancestor::bioghist/descendant::ptr">
								<xsl:if test="@target = $current_id">
									<xsl:value-of select="position()"/>
								</xsl:if>
							</xsl:for-each>
						</xsl:when>
					</xsl:choose>
				</a>
			</div>
			<div style="display:inline;">
				<a name="{@id}"/>
				<xsl:apply-templates/>
			</div>
		</div>

	</xsl:template>

	<xsl:template match="extref">
		<a href="{@href}" target="_blank">
			<xsl:apply-templates/>
		</a>
	</xsl:template>

	<xsl:template match="chronlist | list">
		<ul class="list">
			<xsl:apply-templates/>
		</ul>
	</xsl:template>

	<xsl:template match="chronitem | item">
		<li>
			<xsl:apply-templates/>
		</li>
	</xsl:template>

	<xsl:template match="date">
		<xsl:choose>
			<xsl:when test="parent::chronitem">
				<i>
					<xsl:value-of select="."/>
				</i>
				<xsl:text>:  </xsl:text>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="."/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="event">
		<xsl:apply-templates/>
	</xsl:template>

	<xsl:template match="eventgrp">
		<xsl:for-each select="event">
			<xsl:apply-templates select="."/>
			<xsl:if test="not(position() = last())">
				<xsl:text>; </xsl:text>
			</xsl:if>
		</xsl:for-each>
	</xsl:template>

	<xsl:template match="extptr">
		<xsl:variable name="alignment">
			<xsl:choose>
				<xsl:when test="@altrender='left'">
					<xsl:text>margin-left:0px;</xsl:text>
				</xsl:when>
				<xsl:when test="@altrender='center'">
					<xsl:text>margin-left:auto; margin-right:auto;</xsl:text>
				</xsl:when>
				<xsl:when test="@altrender='right'">
					<xsl:text>margin-right:0px;</xsl:text>
				</xsl:when>
				<xsl:otherwise>
					<xsl:text>margin-left:auto; margin-right:auto;</xsl:text>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>

		<div>
			<table class="extptr_table" style="{$alignment}">
				<tr>
					<td style="text-align:center;">
						<img src="{@href}" alt="{@title}"/>
					</td>
				</tr>
				<tr>
					<td style="text-align:center">
						<div
							style="width:300px; text-align:center; margin-left:auto; margin-right:auto;">
							<xsl:value-of select="@title"/>
						</div>
					</td>
				</tr>
			</table>
		</div>
	</xsl:template>

	<!-- BIBLIOGRAPHY -->
	<xsl:template match="bibliography">
		<xsl:if test="preceding-sibling::did">
			<h3>References</h3>
		</xsl:if>
		<xsl:choose>
			<xsl:when test="not(preceding-sibling::did)">
				<div class="essay_section">
					<a name="{generate-id(.)}"/>
					<xsl:apply-templates/>
					<p>
						<xsl:variable name="section_id">
							<xsl:if test="ancestor::descgrp">
								<xsl:value-of select="generate-id(ancestor::descgrp)"/>
							</xsl:if>
							<xsl:if test="ancestor::bioghist">
								<xsl:value-of select="generate-id(ancestor::bioghist)"/>
							</xsl:if>
						</xsl:variable>
						<xsl:if test="not(parent::node()/parent::archdesc)">
							<a href="#{$section_id}">Back to top</a>
						</xsl:if>
					</p>
				</div>
			</xsl:when>
			<xsl:otherwise>
				<div class="summary_info">
					<xsl:apply-templates/>
				</div>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>


	<xsl:template match="bibref">
		<xsl:variable name="bibref_class">
			<xsl:choose>
				<xsl:when test="preceding-sibling::did">
					<xsl:text>top_bibref</xsl:text>
				</xsl:when>
				<xsl:otherwise>
					<xsl:text>bibref</xsl:text>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<div class="{$bibref_class}">
			<xsl:apply-templates/>
		</div>
	</xsl:template>
	<!-- END BIBLIOGRAPHY -->

	<!-- TABLES -->

	<xsl:template match="table">
		<table class="table">
			<xsl:apply-templates select="head | tgroup"/>
		</table>
	</xsl:template>

	<xsl:template match="tgroup">
		<xsl:apply-templates select="thead | tbody"/>
	</xsl:template>

	<xsl:template match="thead">
		<thead>
			<xsl:apply-templates select="row"/>
		</thead>
	</xsl:template>

	<xsl:template match="tbody">
		<tbody>
			<xsl:apply-templates select="row"/>
		</tbody>
	</xsl:template>

	<xsl:template match="row">
		<tr>
			<xsl:apply-templates select="entry"/>
		</tr>
	</xsl:template>

	<xsl:template match="entry">
		<xsl:choose>
			<xsl:when test="ancestor::thead">
				<th style="text-align:left;">
					<xsl:apply-templates/>
				</th>
			</xsl:when>
			<xsl:otherwise>
				<td>
					<xsl:apply-templates/>
				</td>
			</xsl:otherwise>
		</xsl:choose>

	</xsl:template>

	<!-- END TABLE -->
	<!-- ADMINISTRATIVE HISTORY -->

	<xsl:template match="custodhist">
		<div class="adminhist_div">
			<xsl:choose>
				<xsl:when
					test="not(parent::c[@level='series'] or parent::archdesc) and not(ancestor::archdesc/custodhist or ancestor::c[@level='series']/custodhist)">
					<h4>Provenance: </h4>
					<xsl:apply-templates/>
				</xsl:when>
				<xsl:when test="ancestor::c[@type='level'] or ancestor::archdesc">
					<xsl:choose>
						<xsl:when test="parent::c[@level='item']">
							<h4>Provenance (Item): </h4>
							<xsl:apply-templates/>
						</xsl:when>
						<xsl:when test="parent::c[@level='series']">
							<h4>Provenance (Collection): </h4>
							<xsl:apply-templates/>
						</xsl:when>
						<xsl:when test="parent::archdesc">
							<h4>Provenance (Institutional Repository): </h4>
							<xsl:apply-templates/>
						</xsl:when>
					</xsl:choose>
				</xsl:when>
			</xsl:choose>
		</div>
	</xsl:template>

	<xsl:template match="acqinfo">
		<div class="adminhist_div">
			<xsl:choose>
				<xsl:when
					test="not(parent::c[@level='series'] or parent::archdesc) and not(ancestor::archdesc/acqinfo or ancestor::c[@level='series']/acqinfo)">
					<h4>Acquisition Information: </h4>
					<xsl:apply-templates/>
				</xsl:when>
				<xsl:when test="ancestor::c[@type='level'] or ancestor::archdesc">
					<xsl:choose>
						<xsl:when test="parent::c[@level='item']">
							<h4>Acquisition Information (Item): </h4>
							<xsl:apply-templates/>
						</xsl:when>
						<xsl:when test="parent::c[@level='series']">
							<h4>Acquisition Information (Collection): </h4>
							<xsl:apply-templates/>
						</xsl:when>
						<xsl:when test="parent::archdesc">
							<h4>Acquisition Information (Institutional Repository): </h4>
							<xsl:apply-templates/>
						</xsl:when>
					</xsl:choose>
				</xsl:when>
			</xsl:choose>
		</div>
	</xsl:template>
	
	<!--***************************************** GENERATE THE TABLE OF CONTENTS FOR COMMENTARY **************************************** -->
	
	<xsl:template name="toc">
		<h2>Table of Contents</h2>
		<xsl:for-each select="abstract">
			<div class="essay_toc">
				<a name="{generate-id(.)}"/>
				<h3>
					<xsl:choose>
						<xsl:when test="string(normalize-space(@label))">
							<xsl:value-of select="@label"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:text>Abstract</xsl:text>
						</xsl:otherwise>
					</xsl:choose>
				</h3>
				<a href="#{generate-id(.)}" style="margin-left:20px;">Abstract</a>
			</div>
		</xsl:for-each>
		<xsl:for-each select="descgrp | bioghist">
			<div class="essay_toc">
				<a name="{generate-id(.)}"/>
				<h3>
					<xsl:value-of select="head"/>
					<xsl:if test="string(odd[@type='doc_author'])">
						<xsl:text>, </xsl:text>
						<xsl:value-of select="normalize-space(odd[@type='doc_author'])"/>
					</xsl:if>
				</h3>
				<ul class="toc_list">
					<xsl:call-template name="toc_section_headings">
						<xsl:with-param name="depth" select="1"/>
					</xsl:call-template>
				</ul>
			</div>
		</xsl:for-each>
		<xsl:for-each select="ancestor::c/descgrp | ancestor::c/bioghist">
			<div class="essay_toc">
				<a name="{generate-id(.)}"/>
				<h3>
					<xsl:value-of select="head"/>
					<xsl:if test="string(odd[@type='doc_author'])">
						<xsl:text>, </xsl:text>
						<xsl:value-of select="normalize-space(odd[@type='doc_author'])"/>
					</xsl:if>
				</h3>
				<ul class="toc_list">
					<xsl:call-template name="toc_section_headings">
						<xsl:with-param name="depth" select="1"/>
					</xsl:call-template>
				</ul>
			</div>
		</xsl:for-each>
	</xsl:template>
	
	<xsl:template name="toc_section_headings">
		<xsl:param name="depth"/>
		<xsl:for-each select="scopecontent | bibliography">
			<li style="padding-left:{$depth * 2}0px;">
				<xsl:choose>
					<xsl:when test="string(head)">
						<a href="#{generate-id(.)}">
							<xsl:value-of select="head"/>
						</a>
					</xsl:when>
					<xsl:otherwise>
						<a href="#{generate-id(.)}">Section</a>
					</xsl:otherwise>
				</xsl:choose>
			</li>
			<xsl:if test="scopecontent or bibliography">
				<ul>
					<xsl:variable name="deeper">
						<xsl:value-of select="$depth+1"/>
					</xsl:variable>
					<xsl:call-template name="toc_section_headings">
						<xsl:with-param name="depth" select="$deeper"/>
					</xsl:call-template>
				</ul>
			</xsl:if>
		</xsl:for-each>
	</xsl:template>
	<!--***************************************** END TOC **************************************** -->
	
</xsl:stylesheet>

